package net.togogo.controller;

import net.togogo.bean.User;
import net.togogo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /*注册*/
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String insert (User user){
        userService.insert(user);
        return "/index.jsp";
    }

    /*登录*/
    @RequestMapping("/login")
    public String login(User user){
        User u = userService.login(user);
        if (u!=null){
            return "/list.do";
        }
        return "/login.html";
    }

    /*打印用户列表*/
    @RequestMapping("/list")
    public String list(Model model){
        List<User> users = userService.list();
        users.stream().forEach(System.out::println);
        model.addAttribute("userlist",users);
        return "/list.jsp";
    }

    /*用户详情信息*/
    //localhost:8080/id.do?id=1
    @RequestMapping("/id")
    public String selectOneById(@RequestParam("id") Integer id,Model model){
        User user = userService.selectOneById(id);
        System.out.println("user = " + user);
        model.addAttribute("userlist",user);
        return "/list-id.jsp";
    }
}
